import 'package:flutter/material.dart';
import 'package:flutter_study_jspang/study/DemoListView2.dart';
import 'package:flutter_study_jspang/study/DemoNavigator3.dart';
import 'package:flutter_study_jspang/study02/pages/home_screen.dart';

import '../study/DemoGridView.dart';

/// bottom_navigation_widget.dart

///
/// 自定义，底部导航
///
class BottomNavigationWidget extends StatefulWidget {
  const BottomNavigationWidget({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _BottomNavigationWidgetState();
}

///
/// State API : https://api.flutter-io.cn/flutter/widgets/State-class.html
/// setState method : https://api.flutter-io.cn/flutter/widgets/State/setState.html
///
///
class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  final iconColor = Colors.blue;
  int _currentIndex = 0;
  List<Widget> widgetList = [];

  @override
  void initState() {
    widgetList
      ..add(const HomeScreen())
      ..add(const DemoGridView())
      ..add(const DemoNavigator3())
      ..add(const DemoListView2());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: widgetList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index){
          // setState : https://api.flutter-io.cn/flutter/widgets/State/setState.html
          // If you just change the state directly without calling setState,
          // the framework might not schedule a build and the user interface
          // for this subtree might not be updated to reflect the new state.
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home, color: iconColor,), label: 'Home'),
          BottomNavigationBarItem(icon: Icon(Icons.email, color: iconColor,), label: 'Email'),
          BottomNavigationBarItem(icon: Icon(Icons.pages, color: iconColor,), label: 'Pages'),
          BottomNavigationBarItem(icon: Icon(Icons.airplay, color: iconColor,), label: 'Airplay'),
        ],
      ),
    );
  }
}

